<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function index($str='2025-05-01', $end='2025-05-11'){

        // 开始时间戳 和 结束时间戳
        $startTimestamp = strtotime($str); // 示例开始时间戳
        $endTimestamp   = strtotime($end); ; // 示例结束时间戳，比如：2024-05-01

        // 转换为 DateTime 对象
        $startDate = (new \DateTime())->setTimestamp($startTimestamp);
        $endDate   = (new \DateTime())->setTimestamp($endTimestamp);

        // 创建 DateInterval 对象（1天）
        $interval = new \DateInterval('P1D');

        // 创建 DatePeriod 对象
        $dateRange = new \DatePeriod($startDate, $interval, $endDate);

        // 遍历生成每一天
        $dates = [];
        foreach ($dateRange as $date) {
            $dates[] =strtotime( $date->format('Y-m-d'));
        }

        // 如果你想包含结束日期
        $dates[] =strtotime($endDate->format('Y-m-d'));

        return $dates;
    }

    /**
     * 用户协议
     */

    public function agreement(){


        $info=Db::name('xieyi')->find();

        $this->success('请求成功',$info);
    }


    /**
     * 分类信息
     */
    public function getcate(){

        $list=Db::name('merch_cate')->select();

        $this->success('分类信息',$list);

    }


    /**
     * banner
     */
    public function getbanner()
    {
        $list=Db::name('banner')->select();
        foreach ($list as $k=>$v){
            $list[$k]['image']=cdnurl($v['image'],true);
        }

        $this->success('banner',$list);

    }

    public function getcompinfo(){

        $info=Db::name('compinfo')->find();

        $info['image']=cdnurl($info['image'],true);

        $this->success('公司信息',$info);

    }
    /**
     * 动态信息
     */
    public function getdongtai(){

        $list=Db::name('dongtai')->limit(5)->order('weigh desc')->select();

        foreach ($list as $k=>$v){
            $list[$k]['image']=cdnurl($v['image'],true);
        }

        $this->success('动态信息',$list);
    }

    public function getalldongtai(){

        $list=Db::name('dongtai')->order('weigh desc')->select();

        foreach ($list as $k=>$v){
            $list[$k]['image']=cdnurl($v['image'],true);
        }

        $this->success('动态信息',$list);
    }

    /**
     *
     */
    public function getdongtaibyid()
    {

        $id=$this->request->param('id');

        Db::name('dongtai')->where('id',$id)->setInc('see',1);


        $info=Db::name('dongtai')->where('id',$id)->find();

        $info['image']=cdnurl($info['image'],true);

        $this->success('动态信息',$info);
    }


    public function send()
    {

        $config = get_addon_config('txsms');
        $template = 'notice';
        $sign = $this->request->post('sign', '');

        $mobile='18161043652';

        $templateArr = $config['template'] ?? [];
        if (!isset($templateArr[$template]) || !$templateArr[$template]) {
            $this->error('后台未配置对应的模板');
        }
        $template = $templateArr[$template];
        $signName = $sign ?: $config['signName'];
        $param = (array)json_decode($this->request->post('param', '', 'trim'));




        $length = config('captcha.length');

        $cloudSms = new \addons\txsms\library\TencentCloudSmsClient();
        $ret = $cloudSms->setPhoneNumbers($mobile)
            ->setTemplateId($template)
            ->setSmsSdkAppId($config['smsSdkAppId'])
            ->setSignName($signName)
            ->sendSms();
        if ($ret) {
            $this->success("发送成功");
        } else {
            $error = $cloudSms->getError();
            $this->error("发送短信失败，错误码：" . $error['code'] . "，错误信息：" . $error['message']);
        }
    }



}
